package Aleehot100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: c02
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/20 21:35
 * @Version 1.0
 * @Since 1.0
 */
public class c02 {
    public static void main(String[] args) {
        String[] arr = {"eat","tea","tan","ate","nat","bat"};
        System.out.println(groupAnagrams(arr));
    }


    static HashMap<String, List<String>> map = new HashMap<>();

    /**
     * 字母异位词分组
     * @param strs
     * @return
     */
    public static List<List<String>> groupAnagrams(String[] strs) {
        map.clear();
        // 统计数据
        int index = 0;
        for (int i = 0; i < strs.length; i++) {
            char[] charArray = strs[i].toCharArray();
            Arrays.sort(charArray);
            String str = new String(charArray);
            List<String> list = map.getOrDefault(str, new ArrayList<String>());
            list.add(strs[i]);
            map.put(str, list);
        }
        return new ArrayList<List<String>>(map.values());
    }

}
